/*
 * Copyright © 2010 Siddhanathan S.
 *
 * This file is part of JTeacher.
 *
 * JTeacher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 3 of the License, or
 * any later version.
 *
 * JTeacher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JTeacher.  If not, see <http://www.gnu.org/licenses/>.
 */

package jteacher;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

/**
 * GUI class for building the main graphical user interface
 *
 * @author Siddhanathan S.
 */

public class GUI {
    
    private JFrame frame;
    private JMenuBar menuBar;
    private JMenu file;
    private JMenu edit;
    private JMenu view;
    private JMenu help;
    private JMenuItem clear;
    private JMenuItem quit;
    private JMenuItem bigFont;
    private JMenuItem smallFont;
    private JMenuItem boldFont;
    private JMenuItem removeStyle;
    private JMenuItem wordWrap;
    private JMenuItem about;
    private JTextArea jTextArea;
    private JTree tree;

    private int fontSize = 12;
    private int fontStyle = 0;
    private String font = "Courier";

    private Object[] hierarchy = new Lessons().getTreeStructure();

    public GUI() {
	frame = new JFrame("JTeacher");
	menuBar = new JMenuBar();
	file = new JMenu("File");
	edit = new JMenu("Edit");
	view = new JMenu("View");
	help = new JMenu("Help");
	clear = new JMenuItem("Clear Screen");
	bigFont = new JMenuItem("Increase Font Size");
	smallFont = new JMenuItem("Reduce Font Size");
	boldFont = new JMenuItem("Make Font Style Bold");
	removeStyle = new JMenuItem("Remove any Font Style");
	wordWrap = new JMenuItem("Toggle Word Wrap");
	quit = new JMenuItem("Quit");
	about = new JMenuItem("About");
	jTextArea = new JTextArea();
	tree = new JTree(processHierarchy(hierarchy));
    }

    private void setFont(){
	jTextArea.setFont(new Font(font, fontStyle, fontSize));
    }

    public void gui() {
	
	frame.setPreferredSize(new Dimension(800, 580));
	frame.pack();
        frame.setVisible(true);
	frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setJMenuBar(menuBar);

	menuBar.setOpaque(true);
        menuBar.setPreferredSize(new Dimension(800, 20));
	menuBar.add(file);
	menuBar.add(edit);
	menuBar.add(view);
	menuBar.add(help);

	file.add(quit);

	edit.add(bigFont);
	edit.add(smallFont);
	edit.add(boldFont);
	edit.add(removeStyle);

	view.add(wordWrap);
	view.add(clear);

	help.add(about);

	bigFont.addActionListener(new ActionListener() {

	    public void actionPerformed(ActionEvent e) {
		fontSize++;
		setFont();
	    }
	});

	smallFont.addActionListener(new ActionListener() {

	    public void actionPerformed(ActionEvent e) {
		fontSize++;
		setFont();
	    }
	});

	boldFont.addActionListener(new ActionListener() {

	    public void actionPerformed(ActionEvent e) {
		fontStyle = Font.BOLD;
		setFont();
	    }
	});

	removeStyle.addActionListener(new ActionListener() {

	    public void actionPerformed(ActionEvent e) {
		fontStyle = 0;
		setFont();
	    }
	});

	clear.addActionListener(new ActionListener() {

	    public void actionPerformed(ActionEvent e) {
		jTextArea.setText(null);
	    }
	});

	wordWrap.addActionListener(new ActionListener() {

	    public void actionPerformed(ActionEvent e) {
		if(jTextArea.getLineWrap() == true) {
		    jTextArea.setLineWrap(false);
		} else {
		    jTextArea.setLineWrap(true);
		}
	    }
	});

	quit.addActionListener(new ActionListener() {

	    public void actionPerformed(ActionEvent e) {
		System.exit(0);
	    }
	});

	about.addActionListener(new ActionListener() {

	    public void actionPerformed(ActionEvent e) {
		new About().gui();
	    }
	});

	frame.add(new JSplitPane(
		JSplitPane.HORIZONTAL_SPLIT,
		new JScrollPane(tree),
		new JScrollPane(jTextArea)
	));
	
	jTextArea.setText("Click on any option in the left to begin");
	jTextArea.setEditable(false);
	jTextArea.setWrapStyleWord(true);
	
	tree.addTreeSelectionListener( new TreeSelectionListener() {

	    public void valueChanged(TreeSelectionEvent e) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
		Object nodeInfo = node.getUserObject();
		jTextArea.setText( (new Lessons()).nodeUpdate(nodeInfo) );
	    }
	});

    }

    private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {

	DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
	DefaultMutableTreeNode child;

	for(int i=1; i < (hierarchy.length); i++) {

	    Object nodeSpecifier = hierarchy[i];

	    if (nodeSpecifier instanceof Object[]) { // node with children
	        child = processHierarchy((Object[])nodeSpecifier);
	    } else {
	        child = new DefaultMutableTreeNode(nodeSpecifier); 
	    } // Leaf

	    node.add(child);
	}

	return node;
    }

}
